package com.adopapa.nezhacloud.authc.service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.adopapa.nezhacloud.authc.domain.AddressInfo;
import com.adopapa.nezhacloud.authc.domain.AddressInfoRepository;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.service.BaseDomainService;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;


@Service
public class AddressInfoService extends BaseDomainService<AddressInfo> {

	@Autowired
	private AddressInfoRepository addressInfoRepository;

	public AddressInfo getAddressInfoById(String id) {
		Optional<AddressInfo> optional = addressInfoRepository.findById(id);
		return optional.isPresent() ? optional.get() : null;
	}
	
	public AddressInfo getAddressInfoByLoginName(String loginName) {
		List<AddressInfo> results = addressInfoRepository.findByLoginName(loginName);
		return Tools.empty(results) ? null : results.get(0);
	}

	public Page<AddressInfo> getAddressInfoPage(int pageNo, int pageSize, AddressInfo addressInfo) {
		PageRequest pageRequest = buildPageRequest(pageNo, pageSize, Order.desc("createTime"));
		Specification<AddressInfo> specification = buildSpecification(addressInfo);
		Page<AddressInfo> page = addressInfoRepository.findAll(specification, pageRequest);
		return page;
	}

	public AddressInfo saveAddressInfo(AddressInfo addressInfo) {
		return addressInfoRepository.saveAndFlush(addressInfo);
	}

	public List<AddressInfo> saveAddressInfos(List<AddressInfo> addressInfos) {
		return addressInfoRepository.saveAllAndFlush(addressInfos);
	}

	public void deleteAddressInfo(String id) {
		addressInfoRepository.deleteById(id);
	}

	public void deleteAddressInfo(AddressInfo addressInfo) {
		addressInfoRepository.delete(addressInfo);
	}

	public void deleteAddressInfos(String... ids) {
		addressInfoRepository.deleteAllByIdInBatch(Arrays.asList(ids));
	}

	public void deleteAddressInfos(List<AddressInfo> addressInfos) {
		addressInfoRepository.deleteAllInBatch(addressInfos);
	}

	@SuppressWarnings("serial")
	@Override
	public Specification<AddressInfo> buildSpecification(AddressInfo addressInfo) {
		return new Specification<AddressInfo>() {
			@Override
			public Predicate toPredicate(Root<AddressInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new LinkedList<>();
				if (!Tools.empty(addressInfo.getLoginName())) {
					predicates.add(cb.equal(root.get("loginName"), //
						addressInfo.getLoginName()));
				}
				if (!Tools.empty(addressInfo.getLoginPwd())) {
					predicates.add(cb.equal(root.get("loginPwd"), //
						addressInfo.getLoginPwd()));
				}
				if (!Tools.empty(addressInfo.getUserName())) {
					predicates.add(cb.like(root.get("userName"), //
						"%".concat(addressInfo.getUserName()).concat("%")));
				}
				if (!Tools.empty(addressInfo.getRemark())) {
					predicates.add(cb.equal(root.get("remark"), //
						addressInfo.getRemark()));
				}
				return cb.and(predicates.toArray(new Predicate[0]));
			}

		};
	}
	
	// predicates.add(cb.equal(cb.substring(root.get("datagram"), 1, 23), dataPrefix));

}
